package com.xxh.initial.system.controller;


import com.xxh.initial.system.entity.SysRole;
import com.xxh.initial.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyf
 * @since 2025-04-02
 */
@RestController
@RequestMapping("/system/sysRole")
public class SysRoleController {
    @Autowired
    ISysRoleService sysRoleService;

    @RequestMapping("/test")
    public String test(){
        return "hello world";
    }
    @RequestMapping("/test2")
    public String test2(){
        return "hello world1";
    }

    //页面调用controller的接口，controller调用service的方法，service调用mapper的接口
    /**
     * 新增一个角色
     * @param sysRole Integer
     * @return String
     */
    @PostMapping("/addRole")
    public String addRole(@RequestBody SysRole sysRole){
        //相当于insert into sys_role (role_id, role_name, description) values (?,?,?);
        sysRoleService.save(sysRole);
        return "success";
    }

    /**
     * 删除角色
     * @param roleId Integer
     * @return String
     */
    @DeleteMapping("/delRole/{roleId}")
    public String delRole(@PathVariable Integer roleId){
        /** 相当于delete from sys_role where role_id = ?         */
        sysRoleService.removeById(roleId);
        return "success";
    }

    /**
     * 修改角色
     * @param sysRole SysRole
     * @return String
     */
    @PutMapping("/updataRole")
    public String updataRole(@RequestBody SysRole sysRole){
        //相当于 update sys_role set role_name = ?, description = ? where role_id = ?;
        sysRoleService.updateById(sysRole);
        return "success";
    }

    @GetMapping("/selectRole/{roleId}")
    public SysRole selectRole(@PathVariable Integer roleId){
        //相当于 select * from sys_role where role_id =?;
        SysRole sysRole = sysRoleService.getById(roleId);
        return sysRole;
    }

    /**
     * 新增一个角色
     * @param sysRole Integer
     * @return String
     */
    @PostMapping("/add")
    public String testRoleAdd(@RequestBody SysRole sysRole){
        sysRoleService.testRoleAdd(sysRole);

        return "success";
    }
}
